﻿var globalFrameHeight = 0;
$(function () {
	//隐藏左面菜单
	var switchBarId = "#switchBar";
	$(switchBarId).click(function () {
		if ($("#tdLeftBar").css("display") == "none") {
			$("#tdLeftBar").show();
			$("#leftBar").show("slide");
		} else {
			$("#tdLeftBar").fadeOut();
		}
	});
	loadMenu();
	calculateWindowSize();
	$(window).resize(
				function () {
					calculateWindowSize();
				}
			);
	$(document).keydown(menuShortKeydown).keyup(menuShortKeyup);

});

/* ctrl & esc 快捷键Keydown事件
----------------------------------------------------------*/
var _bind_ctrl_keyup_event = true;
function menuShortKeydown(e) {
	if (e.ctrlKey && e.keyCode != 17) {
		$(document).unbind("keyup", menuShortKeyup);
		_bind_ctrl_keyup_event = false;
		return;
	}
	if (!_bind_ctrl_keyup_event) {
		$(document).bind("keyup", menuShortKeyup);
		_bind_ctrl_keyup_event = !_bind_ctrl_keyup_event;
	}
}

/* ctrl & esc 快捷键Keyup事件
----------------------------------------------------------*/
function menuShortKeyup(e) {
	if (e.keyCode == 27) {
		var hasExpendSecond = false;
		$(window.top.document).find("#menu dl").each(
					function () {
						if (hasExpendSecond)
							return;
						hasExpendSecond = $(this).css("display") == "block";
					}
				);
		if (hasExpendSecond) {
			$(window.top.document).find("#menu dl").hide();
		}
		else {
			$(window.top.document).find("#tdLeftBar").fadeOut();
		}
	}
	if (e.keyCode == 17) {
		if ($(window.top.document).find("#tdLeftBar").css("display") == "none") {
			$(window.top.document).find("#tdLeftBar").show();
			$(window.top.document).find("#leftBar").show("slide");
			return;
		}
		else {
			if ($(window.top.document).find("#menu .dt_selected").length == 0) {
				$(window.top.document).find("#menu .dt").first().click();
			} else {
				$(window.top.document).find("#menu .dt_selected").click();
			}
		}
	}
}

/* 加载菜单脚本
----------------------------------------------------------*/
function loadMenu() {
	var menuId = "#menu";
	//所有二级菜单隐藏
	$(menuId).find("dl").hide();

	//点击事件
	$(menuId).find(".dt").filter(function () { return $(this).parent().find("dd").size() != 0 }).click(function () {
		if ($(this).parent().find("dl").css("display") == "block") {
			$(this).parent().find("dl").fadeOut();
		} else {
			$(this).parent().find("dl").height($("#leftBar").height());
			$("#menu .dt").removeClass("on").removeClass("nosmon").removeClass("dt_selected");
			$("#menu dl").hide();
			$(this).addClass("on").addClass("dt_selected");
			$(this).parent().find("dl").show("slide");
		}
	})
	//二级菜单点击样式
	$(menuId).find("dd").click(function () { $("dd").removeClass("focus"); $(this).addClass("focus"); });

}

/* 首页主体区域URL重定向
----------------------------------------------------------*/
function setframeUrl(url, menu) {
	$("#mainFrame").attr("src", url);
	$("#navigation").html(menu);
	$("#menu dl").hide('fast');
}

/* 计算每部分内容尺寸
----------------------------------------------------------*/
function calculateWindowSize() {
	$("body").height($("html").height());
	var menuHeight = $("body").height() - $("#topContainer").height();
	$("#tdMainContent").height(menuHeight);
	$("#mainContent").height(menuHeight);
	$("#menuTr").height(menuHeight);
	$("#menu").height(menuHeight);
	//$("#leftBar").height(menuHeight - 5);
	$("#mainFrame").height(menuHeight - $("#navigation").height() - $("#copyright").height());
	globalFrameHeight = $("#mainFrame").height();
}


function showFrameDialog(url, title, width, height, beforeClose) {
	if ($("#dialog #iframeDialog")[0].contentWindow.itpOverlay)
		$("#dialog #iframeDialog")[0].contentWindow.itpOverlay.show();
	width = width == undefined || width == null ? 650 : width;
	height = height == undefined || height == null ? 550 : height;
	if ($.browser.msie)
		height += 160;
	$("#iframeDialog").attr("src", url);
	$("#dialog").dialog({ modal: true, title: title, width: width, height: height, canMinimize: true, canMaximize: false, beforeClose: beforeClose });
}

/* 从框架页面打开模态窗口
----------------------------------------------------------*/
var dialogTwo = null;
function showFrameDialogTwo(url, title, width, height, beforeClose) {
	if ($("#dialogTwo #iframeDialogTwo")[0].contentWindow.itpOverlay)
		$("#dialogTwo #iframeDialogTwo")[0].contentWindow.itpOverlay.show();
	width = width == undefined || width == null ? 650 : width;
	height = height == undefined || height == null ? 550 : height;
	if ($.browser.msie)
		height += 160;
	$("#iframeDialogTwo").attr("src", url);
	dialogTwo = $("#dialogTwo").dialog({ modal: true, title: title, width: width, height: height, canMinimize: true, canMaximize: false, beforeClose: beforeClose });
}

/* 从框架页面打开模态窗口
----------------------------------------------------------*/
var dialogThree = null;
function showFrameDialogThree(url, title, width, height, beforeClose) {
	if ($("#dialogThree #iframeDialogThree")[0].contentWindow.itpOverlay)
		$("#dialogThree #iframeDialogThree")[0].contentWindow.itpOverlay.show();
	width = width == undefined || width == null ? 650 : width;
	height = height == undefined || height == null ? 550 : height;
	if ($.browser.msie)
		height += 160;
	$("#iframeDialogThree").attr("src", url);
	dialogTwo = $("#dialogThree").dialog({ modal: true, title: title, width: width, height: height, canMinimize: true, canMaximize: false, beforeClose: beforeClose });
}


/* 关闭框架层
----------------------------------------------------------*/
function closeDialog(isRefresh, message, dialogId, callback, notShowMessage) {
	if (isRefresh == "refresh") {
		var mainFrame = document.getElementById("mainFrame");
		//mainFrame.src = mainFrame.src + "/?time=" + new Date();
		var contentWindow = $("#mainFrame")[0].contentWindow;
		if (contentWindow.commonPage.getCurrentUrl() != undefined && contentWindow.commonPage.getCurrentUrl() != null)
			contentWindow.location = contentWindow.commonPage.getCurrentUrl();
		else
			contentWindow.location = contentWindow.location;
		if (!notShowMessage)
			showSysMessage(message);
	}
	dialogId = dialogId == undefined || dialogId == null ? "#dialog" : "#" + dialogId;
	if (callback != undefined && callback != "")
		window.eval(callback + '();');
	$(dialogId).dialog("close");
}

/* 刷新框架层
----------------------------------------------------------*/
function refreshDialog() {
	$("#iframeDialog").contents()[0].location = $("#iframeDialog").contents()[0].location;
}

/* 关闭并刷新二级iframe  
二级iframe ID为：pageRightFrame
----------------------------------------------------------*/
function closePageRightFrameDialog(isRefresh, message) {
	if (isRefresh == "refresh") {
		/* 下策，企业信息修改、商铺信息修改。
		----------------------------------------------------------*/
		if ($("#mainFrame").contents().find("#pageRightFrame").length == 0) {
			closeDialog('refresh');
			return;
		}

		var contentWindow = $("#mainFrame").contents().find("#pageRightFrame")[0].contentWindow;
		if (contentWindow.commonPage.getCurrentUrl() != undefined && contentWindow.commonPage.getCurrentUrl() != null)
			contentWindow.location = contentWindow.commonPage.getCurrentUrl();
		else
			contentWindow.location = contentWindow.location;

		showSysMessage(message);
	}
	$("#dialog").dialog("close");
}

/* 关闭页面上弹出的两个dialog
----------------------------------------------------------*/
function closePageDialogTwo(isRefresh, message) {
	$("#dialogTwo").dialog("close");

	if (isRefresh == "refresh") {
		$("#iframeDialog")[0].contentWindow.location = ($("#iframeDialog")[0].contentWindow.location.toString().indexOf('?') > 0 ? $("#iframeDialog")[0].contentWindow.location + "&time=" + new Date() : $("#iframeDialog")[0].contentWindow.location + "?time=" + new Date());
		showSysMessage(message);
	}
}

function closePageDialogThree(isRefresh, message) {
	$("#dialogThree").dialog("close");

	if (isRefresh == "refresh") {
		$("#iframeDialog")[0].contentWindow.location = ($("#iframeDialog")[0].contentWindow.location.toString().indexOf('?') > 0 ? $("#iframeDialog")[0].contentWindow.location + "&time=" + new Date() : $("#iframeDialog")[0].contentWindow.location + "?time=" + new Date());
		showSysMessage(message);
	}
}

/* 刷新资产管理--> 室信息维护、户型信息 -> 增加部位
----------------------------------------------------------*/
function refreshRoomPart() {
	$("#iframeDialog").contents().find('#iframeRoomPart')[0].contentWindow.location.reload(true);
}

/* 刷新资产管理--> 室信息维护、户型信息 -> 增加部位资产刷新
----------------------------------------------------------*/
function refreshRoomPartAsset() {
	$("#iframeDialog").contents().find('#iframeRoomPartAsset')[0].contentWindow.location.reload(true);
}

/* 右下角弹出系统消息
----------------------------------------------------------*/
function showSysMessage(message) {
	if (message != undefined && message != "")
		$("#sysMessage").html(message);
	var left = $("html").width() - $("#sysMessage").outerWidth();
	var top = $("html").height() - $("#sysMessage").outerHeight() - 10;
	$("#sysMessage").css("left", left + "px");
	$("#sysMessage").css("top", top + "px");
	$("#sysMessage").css("display", "block");
	$("#sysMessage").slideDown();
	window.setTimeout("$('#sysMessage').fadeOut();", 2000);

}

/* 错误消息提示
----------------------------------------------------------*/
function showSysAlert(message) {
	if (message == undefined)
		return;
	$("#sysAlert").html(message);
	$("#sysAlert").dialog({ modal: true, title: "系统提示", height: "200" });
	$("#sysAlert").dialog({
		modal: true,
		buttons: {
			确定: function () {
				$(this).dialog("close");
			}
		}
	});
}

function redirectToLogin() {
	window.top.location = "/SysLoginManage/index";
}

var _temp_cache_key = null;
function setCacheValue(value) {
	var date = new Date();
	var ticks = date.getTime() + "_value";
	window[ticks] = value;
	_temp_cache_key = ticks;
}

function getCacheValue(isKeep) {
	var temp = window[_temp_cache_key];
	if (!isKeep)
		window[_temp_cache_key] = null;
	return temp;
}

var _temp_callback_key = null;
function setCallBackFun(fun) {
	var date = new Date();
	var ticks = date.getTime() + "_callback";
	window[ticks] = fun;
	_temp_callback_key = ticks;
}

function getCallBackFun(isKeep) {
	var temp = window[_temp_callback_key];
	if (!isKeep)
		window[_temp_callback_key] = null;
	return temp;
}






